package com.v1.video.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.iss.view.pulltorefresh.PullToRefreshBase;
import com.iss.view.pulltorefresh.PullToRefreshBase.Mode;
import com.iss.view.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import com.iss.view.pulltorefresh.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.adapter.BuddhismDetailAdapter;
import com.v1.video.domain.ThemeDetailEntity;
import com.v1.video.httpApi.V1VideoHttpApi;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;

/**
 * 佛教详情
 * 
 * @author wangfang
 * 
 */
public class BuddhismDetailActivity extends BaseActivity implements
		OnClickListener {

	private final String TAG = "BuddhismDetailActivity";
	private View mViewHead;
	private PullToRefreshListView mRefreshListView;
	private ListView mListView;
	private TextView mTvTitle, mTvContent, mTvMore;
	private String mThemdId;
	private ImageView mIvImage;
	private BuddhismDetailAdapter mAdapter;
	private int mPageIndex = 1;
	private boolean IsDevelop = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buddhismdetail);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mViewHead = getLayoutInflater().inflate(
				R.layout.activity_buddhismdetail_header, null);
		mTvTitle = (TextView) this.findViewById(R.id.tv_title);
		mRefreshListView = (PullToRefreshListView) this
				.findViewById(R.id.lv_listView);
		mRefreshListView.setScrollingWhileRefreshingEnabled(false);
		mRefreshListView.setMode(Mode.PULL_FROM_START);
		mListView = mRefreshListView.getRefreshableView();
		mListView.addHeaderView(mViewHead);
		mIvImage = (ImageView) mViewHead.findViewById(R.id.iv_image);
		mTvContent = (TextView) mViewHead.findViewById(R.id.tv_content);
		mTvMore = (TextView) mViewHead.findViewById(R.id.tv_more);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		Intent intent = this.getIntent();
		String title = intent.getStringExtra("title");
		mThemdId = intent.getStringExtra("themdId");
		String imgUrl = intent.getStringExtra("imgUrl");
		String content = intent.getStringExtra("content");
		if (!TextUtils.isEmpty(title)) {
			mTvTitle.setText(title);
		}
		if (!TextUtils.isEmpty(imgUrl)) {
			ImageLoader.getInstance().displayImage(imgUrl, mIvImage,
					Constant.VIDEO_10_16_OPTION, null);
		}
		if (!TextUtils.isEmpty(content)) {
			mTvContent.setText(content);
		} else {
			mTvContent.setVisibility(View.GONE);
			mTvMore.setVisibility(View.GONE);
		}
		setContractionExpansion();
		mAdapter = new BuddhismDetailAdapter(this);
		mListView.setAdapter(mAdapter);
		if (Helper.checkConnection(BuddhismDetailActivity.this)) {
			getServerData();
		} else {
			Toast.makeText(BuddhismDetailActivity.this,
					getResources().getString(R.string.net_no_notclick),
					Toast.LENGTH_SHORT).show();
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.lay_result).setOnClickListener(this);
		mTvMore.setOnClickListener(this);
		mRefreshListView.setOnRefreshListener(new OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(BuddhismDetailActivity.this)) {
					mPageIndex = 1;
					getServerData();
				} else {
					if (mRefreshListView != null) {
						mRefreshListView.onRefreshComplete();
					}
					Toast.makeText(BuddhismDetailActivity.this,
							getResources().getString(R.string.net_no_notclick),
							Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(BuddhismDetailActivity.this)) {
					getServerData();
				} else {
					if (mRefreshListView != null) {
						mRefreshListView.onRefreshComplete();
					}
					Toast.makeText(BuddhismDetailActivity.this,
							getResources().getString(R.string.net_no_notclick),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lay_result:
			this.finish();
			break;
		case R.id.tv_more:
			 if (IsDevelop) {
                 IsDevelop = false;
             } else {
                 IsDevelop = true;
             }
             setContractionExpansion();
			break;
		default:
			break;
		}
	}

	private void setContractionExpansion() {
		if (IsDevelop) {
			Drawable drawable = getResources().getDrawable(
					R.drawable.icon_buddhismdetail_shrink);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(),
					drawable.getMinimumHeight());
			mTvMore.setCompoundDrawables(drawable, null, null, null);
			mTvContent.setMaxLines(100);
			mTvMore.setText(R.string.buddhismdetail_shrink_text);
		} else {
			Drawable drawable = getResources().getDrawable(
					R.drawable.icon_buddhismdetail_deploy);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(),
					drawable.getMinimumHeight());
			mTvMore.setCompoundDrawables(drawable, null, null, null);
			mTvContent.setMaxLines(3);
			mTvMore.setText(R.string.buddhismdetail_deploy_text);
		}
	}

	/**
	 * 获取服务器数据
	 */
	private void getServerData() {
		String url = String.format(V1VideoHttpApi.BUDDHISMDETAIL_VIDEO_LIST(),
				mThemdId, mPageIndex, Constant.DEVICEID);
		Logger.i(TAG, "佛教详情Url=" + url);
		RequestManager.getInstance().getRequest(this, url,
				ThemeDetailEntity.class, new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						// TODO Auto-generated method stub
						if (mRefreshListView != null) {
							mRefreshListView.onRefreshComplete();
						}
						if (object != null) {
							try {
								ThemeDetailEntity entity = (ThemeDetailEntity) object;
								if (entity != null && entity.getBody() != null
										&& entity.getBody().getData() != null) {
									if (mAdapter != null) {
										if (mPageIndex == 1) {
											mAdapter.setLstData(entity
													.getBody().getData());
										} else {
											mAdapter.setAddLstData(entity
													.getBody().getData());
										}
									}
									int total=Integer.parseInt(entity.getBody().getPage());
									if (mPageIndex < total) {
										mPageIndex++;
										if (mRefreshListView != null) {
											mRefreshListView.setMode(Mode.BOTH);
										}
									} else {
										if (mRefreshListView != null) {
											mRefreshListView
													.setMode(Mode.PULL_FROM_START);
										}
									}
								}
							} catch (Exception ex) {
								ex.printStackTrace();
								Logger.e(TAG, ex.getMessage());
							}
						}
					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						if (mRefreshListView != null) {
							mRefreshListView.onRefreshComplete();
						}
						Toast.makeText(BuddhismDetailActivity.this, message,
								Toast.LENGTH_SHORT).show();
					}
				});
	}
}
